/***************************************************************************************************************************************
 * Copyright (C) 2001-2012 LearnLift USA																	*
 * Contact: Learnlift USA, 12 Greenway Plaza, Suite 1510, Houston, Texas 77046, support@memorylifter.com					*
 *																								*
 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License	*
 * as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.			*
 *																								*
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty	*
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.	*
 *																								*
 * You should have received a copy of the GNU Lesser General Public License along with this library; if not,					*
 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA					*
 ***************************************************************************************************************************************/
// The following code was generated by Microsoft Visual Studio 2005.
// The test owner should check each test for validity.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Text;
using System.Collections.Generic;
using System.IO;
using MLifter.BusinessLayer;
using MLifter.Generics;
namespace MLifterTest
{
	/// <summary>
	///This is a test class for MLifter.BusinessLayer.ConnectionStringProperty and is intended
	///to contain all MLifter.BusinessLayer.ConnectionStringProperty Unit Tests
	///</summary>
	[TestClass()]
	public class ConnectionStringPropertyTest
	{


		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}
		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		//
		//[ClassInitialize()]
		//public static void MyClassInitialize(TestContext testContext)
		//{
		//}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		//
		//[ClassCleanup()]
		//public static void MyClassCleanup()
		//{
		//}
		//
		//Use TestInitialize to run code before running each test
		//
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//
		#endregion


	}
	/// <summary>
	///This is a test class for MLifter.BusinessLayer.ConnectionStringHandler and is intended
	///to contain all MLifter.BusinessLayer.ConnectionStringHandler Unit Tests
	///</summary>
	[TestClass()]
	public class ConnectionStringHandlerTest
	{


		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}
		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		//
		//[ClassInitialize()]
		//public static void MyClassInitialize(TestContext testContext)
		//{
		//}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		//
		//[ClassCleanup()]
		//public static void MyClassCleanup()
		//{
		//}
		//
		//Use TestInitialize to run code before running each test
		//
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//
		#endregion


		/// <summary>
		/// A test for ConnectionStringHandler (string, string)
		/// </summary>
		[TestMethod()]
		public void CSHConstructorTestNull()
		{
			string generalPath = null;
			string userPath = null;

			ConnectionStringHandler target = new ConnectionStringHandler(generalPath, userPath);
			Assert.IsTrue(target.ConnectionStrings.FindAll(c => !(c is UncConnectionStringBuilder) || !(c as UncConnectionStringBuilder).IsOnStick).Count == 0, "CSHConstructorTest - There should be 0 connection strings!");
		}

		/// <summary>
		/// A test for ConnectionStringHandler (string, string)
		/// </summary>
		[TestMethod()]
		public void CSHConstructorTestEmpty()
		{
			string generalPath = string.Empty;
			string userPath = string.Empty;

			ConnectionStringHandler target = new ConnectionStringHandler(generalPath, userPath);
			Assert.IsTrue(target.ConnectionStrings.FindAll(c => !(c is UncConnectionStringBuilder) || !(c as UncConnectionStringBuilder).IsOnStick).Count == 0, "CSHConstructorTest - There should be 0 connection strings!");
		}

		/// <summary>
		///A test for ConnectionStringHandler (string, string)
		///</summary>
		[TestMethod()]
		public void CSHConstructorTest()
		{
			Dictionary<string, string> referenceValues1, referenceValues2, referenceValues3, referenceValues4, referenceValues5, referenceValues6;

			string generalPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
			string userPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

			Directory.CreateDirectory(generalPath);
			Directory.CreateDirectory(userPath);

			ConnectionStringHandler.CreateUncConnection(PrepareUncConnectionStringBuilder(out referenceValues1), generalPath, "unc1.mlcfg");
			ConnectionStringHandler.CreateUncConnection(PrepareUncConnectionStringBuilder(out referenceValues2), generalPath, "unc2.mlcfg");
			ConnectionStringHandler.CreatePostgreSqlConnection(PreparePostgreSqlConnectionStringBuilder(out referenceValues3), generalPath, "pg1.mlcfg");

			ConnectionStringHandler.CreateUncConnection(PrepareUncConnectionStringBuilder(out referenceValues4), userPath, "unc1.mlcfg");
			ConnectionStringHandler.CreatePostgreSqlConnection(PreparePostgreSqlConnectionStringBuilder(out referenceValues5), userPath, "pg1.mlcfg");
			ConnectionStringHandler.CreatePostgreSqlConnection(PreparePostgreSqlConnectionStringBuilder(out referenceValues6), userPath, "pg2.mlcfg");
			ConnectionStringHandler target = new ConnectionStringHandler(generalPath, userPath);
			Assert.IsTrue(target.ConnectionStrings.FindAll(c => !(c is UncConnectionStringBuilder) || !(c as UncConnectionStringBuilder).IsOnStick).Count == 2, "CSHConstructorTest - There should be 6 connection strings!");
			foreach (IConnectionString csb in target.ConnectionStrings.FindAll(c => !(c is UncConnectionStringBuilder) || !(c as UncConnectionStringBuilder).IsOnStick))
			{
				if (csb.ConnectionType == MLifter.DAL.DatabaseType.PostgreSQL)
				{
					Assert.AreEqual<MLifter.DAL.DatabaseType>(MLifter.DAL.DatabaseType.PostgreSQL, csb.ConnectionType, "CSHPostgreSqlConnectionStringBuilderTest - ConnectionType is not correct!");
					Assert.AreEqual<string>(referenceValues3["Name"], csb.Name, "CSHPostgreSqlConnectionStringBuilderTest - Name is not correct!");
				}
				else
				{
					Assert.AreEqual<MLifter.DAL.DatabaseType>(MLifter.DAL.DatabaseType.Unc, csb.ConnectionType, "CSHUncConnectionStringBuilderTest - ConnectionType is not correct!");
					Assert.AreEqual<string>(referenceValues1["Name"], csb.Name, "CSHUncConnectionStringBuilderTest - Name is not correct!");
					Assert.AreEqual<string>(referenceValues1["ConnectionString"], csb.ConnectionString, "CSHUncConnectionStringBuilderTest - ConnectionString is not correct!");
				}
			}

			Directory.Delete(generalPath, true);
			Directory.Delete(userPath, true);
		}

		/// <summary>
		/// A test for CreateUncConnection.
		/// </summary>
		/// <remarks>Documented by Dev03, 2008-12-19</remarks>
		[TestMethod()]
		[ExpectedException(typeof(ArgumentNullException))]
		public void CSGCreateUncConnectionNullTest()
		{
			ConnectionStringHandler.CreateUncConnection(null, "test", "test");
		}

		/// <summary>
		/// A test for CreateUncConnection.
		/// </summary>
		/// <remarks>Documented by Dev03, 2008-12-19</remarks>
		[TestMethod()]
		[ExpectedException(typeof(ArgumentNullException))]
		public void CSGCreateUncConnectionNullParam2Test()
		{
			Dictionary<string, string> referenceValues;
			UncConnectionStringBuilder csb = PrepareUncConnectionStringBuilder(out referenceValues);
			ConnectionStringHandler.CreateUncConnection(csb, null, "test");
		}

		/// <summary>
		/// A test for CreateUncConnection.
		/// </summary>
		/// <remarks>Documented by Dev03, 2008-12-19</remarks>
		[TestMethod()]
		[ExpectedException(typeof(ArgumentNullException))]
		public void CSGCreateUncConnectionNullParam3Test()
		{
			Dictionary<string, string> referenceValues;
			UncConnectionStringBuilder csb = PrepareUncConnectionStringBuilder(out referenceValues);
			ConnectionStringHandler.CreateUncConnection(csb, "test", null);
		}

		/// <summary>
		/// A test for CreateUncConnection.
		/// </summary>
		/// <remarks>Documented by Dev03, 2008-12-19</remarks>
		[TestMethod()]
		[ExpectedException(typeof(ArgumentException))]
		public void CSGCreateUncConnectionEmptyParam2Test()
		{
			Dictionary<string, string> referenceValues;
			UncConnectionStringBuilder csb = PrepareUncConnectionStringBuilder(out referenceValues);
			ConnectionStringHandler.CreateUncConnection(csb, " ", "test");
		}

		/// <summary>
		/// A test for CreateUncConnection.
		/// </summary>
		/// <remarks>Documented by Dev03, 2008-12-19</remarks>
		[TestMethod()]
		[ExpectedException(typeof(ArgumentException))]
		public void CSGCreateUncConnectionEmptyParam3Test()
		{
			Dictionary<string, string> referenceValues;
			UncConnectionStringBuilder csb = PrepareUncConnectionStringBuilder(out referenceValues);
			ConnectionStringHandler.CreateUncConnection(csb, "test", " ");
		}

		/// <summary>
		/// Tests that USB stick connections use the MEMSTICK placeholder in the file and replace it properly with drive letter once loaded.
		/// </summary>
		/// <remarks>Documented by Dev09, 2009-04-02</remarks>
		[TestMethod(), TestCategory("Inconclusive")]
		public void UncStickCSBReadWriteTest()
		{
			List<DriveInfo> drives = Methods.GetMLifterSticks();
			if (drives.Count < 1)
				Assert.Inconclusive("No Stick plugged in! To test the stick functionality please plug in a stick!");

			string LMPath = Path.Combine(drives[0].RootDirectory.ToString(), "LearningModules");
			UncConnectionStringBuilder stick = new UncConnectionStringBuilder(Path.Combine(drives[0].RootDirectory.ToString(), "LearningModules"), false, true);
			ConnectionStringHandler.CreateUncConnection("StickTest", LMPath, drives[0].RootDirectory.ToString(), "defaultConnection.mlcfg", false, true);

			// test that the file was generated with the proper placeholder
			StreamReader sreader = new StreamReader(drives[0].RootDirectory.ToString() + "defaultConnection.mlcfg");
			Assert.AreNotEqual(-1, sreader.ReadToEnd().IndexOf("MEMSTICK:\\"), "MEMSTICK identifier not found");
			sreader.Close();

			// test that reading the file translates to the proper drive letter
			ConnectionStringHandler handler = new ConnectionStringHandler(drives[0].RootDirectory.ToString(), string.Empty);
			IConnectionString connection = handler.ConnectionStrings.Find(c => c.Name == "StickTest");
			Assert.IsNotNull(connection, "MEMSTICK config file not generated");
			Assert.AreEqual<string>(drives[0].RootDirectory.ToString() + "defaultConnection.mlcfg", connection.ConfigFileName);

			File.Delete(Path.Combine(drives[0].RootDirectory.ToString(), "defaultConnection.mlcfg"));
		}

		/// <summary>
		/// A test for CreatePostgreSqlConnection.
		/// </summary>
		/// <remarks>Documented by Dev03, 2008-12-19</remarks>
		[TestMethod()]
		[ExpectedException(typeof(ArgumentNullException))]
		public void CSGCreatePgSqlConnectionNullTest()
		{
			ConnectionStringHandler.CreatePostgreSqlConnection(null, "test", "test");
		}

		/// <summary>
		/// A test for CreatePostgreSqlConnection.
		/// </summary>
		/// <remarks>Documented by Dev03, 2008-12-19</remarks>
		[TestMethod()]
		[ExpectedException(typeof(ArgumentNullException))]
		public void CSGCreatePgSqlConnectionNullParam2Test()
		{
			Dictionary<string, string> referenceValues;
			PostgreSqlConnectionStringBuilder csb = PreparePostgreSqlConnectionStringBuilder(out referenceValues);
			ConnectionStringHandler.CreatePostgreSqlConnection(csb, null, "test");
		}

		/// <summary>
		/// A test for CreatePostgreSqlConnection.
		/// </summary>
		/// <remarks>Documented by Dev03, 2008-12-19</remarks>
		[TestMethod()]
		[ExpectedException(typeof(ArgumentNullException))]
		public void CSGCreatePgSqlConnectionNullParam3Test()
		{
			Dictionary<string, string> referenceValues;
			PostgreSqlConnectionStringBuilder csb = PreparePostgreSqlConnectionStringBuilder(out referenceValues);
			ConnectionStringHandler.CreatePostgreSqlConnection(csb, "test", null);
		}

		/// <summary>
		/// A test for CreatePostgreSqlConnection.
		/// </summary>
		/// <remarks>Documented by Dev03, 2008-12-19</remarks>
		[TestMethod()]
		[ExpectedException(typeof(ArgumentException))]
		public void CSGCreatePgSqlConnectionEmptyParam2Test()
		{
			Dictionary<string, string> referenceValues;
			PostgreSqlConnectionStringBuilder csb = PreparePostgreSqlConnectionStringBuilder(out referenceValues);
			ConnectionStringHandler.CreatePostgreSqlConnection(csb, " ", "test");
		}

		/// <summary>
		/// A test for CreatePostgreSqlConnection.
		/// </summary>
		/// <remarks>Documented by Dev03, 2008-12-19</remarks>
		[TestMethod()]
		[ExpectedException(typeof(ArgumentException))]
		public void CSGCreatePgSqlConnectionEmptyParam3Test()
		{
			Dictionary<string, string> referenceValues;
			PostgreSqlConnectionStringBuilder csb = PreparePostgreSqlConnectionStringBuilder(out referenceValues);
			ConnectionStringHandler.CreatePostgreSqlConnection(csb, "test", " ");
		}

		/// <summary>
		///A test for UncConnectionStringBuilder
		///</summary>
		[TestMethod()]
		public void CSHUncConnectionStringBuilderTest()
		{
			Dictionary<string, string> referenceValues;
			UncConnectionStringBuilder csb = PrepareUncConnectionStringBuilder(out referenceValues);
			Assert.AreEqual<MLifter.DAL.DatabaseType>(MLifter.DAL.DatabaseType.Unc, csb.ConnectionType, "CSHUncConnectionStringBuilderTest - ConnectionType is not correct!");
			Assert.AreEqual<string>(referenceValues["Name"], csb.Name, "CSHUncConnectionStringBuilderTest - Name is not correct!");
			Assert.AreEqual<string>(referenceValues["ConnectionString"], csb.ConnectionString, "CSHUncConnectionStringBuilderTest - ConnectionString is not correct!");
		}

		/// <summary>
		///A test for UncConnectionStringBuilder
		///</summary>
		[TestMethod()]
		public void CSHPostgreSqlConnectionStringBuilderTest()
		{
			Dictionary<string, string> referenceValues;
			PostgreSqlConnectionStringBuilder csb = PreparePostgreSqlConnectionStringBuilder(out referenceValues);
			Assert.AreEqual<MLifter.DAL.DatabaseType>(MLifter.DAL.DatabaseType.PostgreSQL, csb.ConnectionType, "CSHPostgreSqlConnectionStringBuilderTest - ConnectionType is not correct!");
			Assert.AreEqual<string>(referenceValues["Name"], csb.Name, "CSHPostgreSqlConnectionStringBuilderTest - Name is not correct!");
		}

		private UncConnectionStringBuilder PrepareUncConnectionStringBuilder(out Dictionary<string, string> referenceValues)
		{
			referenceValues = new Dictionary<string, string>();
			referenceValues.Add("ConnectionString", @"c:\lms");
			referenceValues.Add("Name", "Test ConnectionString");

			UncConnectionStringBuilder csb = new UncConnectionStringBuilder(referenceValues["ConnectionString"]);
			csb.Name = referenceValues["Name"];
			return csb;
		}

		private PostgreSqlConnectionStringBuilder PreparePostgreSqlConnectionStringBuilder(out Dictionary<string, string> referenceValues)
		{
			referenceValues = new Dictionary<string, string>();
			referenceValues.Add("Name", "Test ConnectionString");
			referenceValues.Add("Database", "db");
			referenceValues.Add("Server", "server");
			referenceValues.Add("Port", "1234");
			referenceValues.Add("SSL", false.ToString());
			referenceValues.Add("UserId", "username");
			referenceValues.Add("Password", "password");

			PostgreSqlConnectionStringBuilder csb = new PostgreSqlConnectionStringBuilder();
			csb.Name = referenceValues["Name"];
			csb.Database = referenceValues["Database"];
			csb.Server = referenceValues["Server"];
			csb.Port = int.Parse(referenceValues["Port"]);
			csb.SSL = bool.Parse(referenceValues["SSL"]);
			csb.UserId = referenceValues["UserId"];
			csb.Password = referenceValues["Password"];
			return csb;
		}
	}
}
